package com.ocm.tourism.web;

import java.io.UnsupportedEncodingException;
import java.sql.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.util.UriUtils;
import org.springframework.web.util.WebUtils;

import com.ocm.tourism.core.domain.TourType;
import com.ocm.tourism.core.service.CommonItemsService;
import com.ocm.tourism.core.service.ScheduleTourService;
import com.ocm.tourism.core.util.TourismUtil;
import com.ocm.tourism.web.util.FileInfoUtil;

@RequestMapping("/advs")
@Controller
public class AdvertisementController {

   @Autowired
   ScheduleTourService scheduleTourService;

   @Autowired
   CommonItemsService commonItemsService;

   @Autowired
   FileInfoUtil fileInfoUtil;

   @InitBinder
   public void initBinder(WebDataBinder binder) {
        binder.initDirectFieldAccess();
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }

   @RequestMapping(value="/touristAgency", produces = "text/html", method=RequestMethod.GET)
   public String listAdvs(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, Model uiModel) {
      long count = scheduleTourService.countTayNinhTourAdvs();
      page = page == null ? 1 : page; 
      int sizeNo = size == null ? 25 : size.intValue();
      final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
      uiModel.addAttribute("scheduleTours", scheduleTourService.findScheduleTourAdvs(firstResult, sizeNo));
      float nrOfPages = (float) count / sizeNo;
      int maxPages = (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages);
      uiModel.addAttribute("maxPages", maxPages);
      uiModel.addAttribute("nextPage", TourismUtil.getNextPage(page, maxPages));
      return "touristAgency/advs/tayninhtouradv";
   }
   
   @RequestMapping(value="/otherTouristAgency", produces = "text/html", method=RequestMethod.GET)
   public String listOthersAdvs(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, Model uiModel) {
      long count = scheduleTourService.countOtherTayNinhTourAdvs();
      page = page == null ? 1 : page; 
      int sizeNo = size == null ? 25 : size.intValue();
      final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
      uiModel.addAttribute("scheduleTours", scheduleTourService.findOthersScheduleTourAdvs(firstResult, sizeNo));
      float nrOfPages = (float) count / sizeNo;
      int maxPages = (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages);
      uiModel.addAttribute("maxPages", maxPages);
      uiModel.addAttribute("nextPage", TourismUtil.getNextPage(page, maxPages));
      return "touristAgency/advs/othertayninhtouradv";
   }

   
   @RequestMapping(value="/tayninhtour", produces = "text/html", method=RequestMethod.GET)
   public String listTayNinhTour(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, Model uiModel) {
      long count = scheduleTourService.countToursByTourType(TourType.TAYNINH);
      page = page == null ? 1 : page; 
      int sizeNo = size == null ? 25 : size.intValue();
      final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
      uiModel.addAttribute("scheduleTours", scheduleTourService.findToursByTourType(TourType.TAYNINH, firstResult, sizeNo));
      float nrOfPages = (float) count / sizeNo;
      int maxPages = (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages);
      uiModel.addAttribute("maxPages", maxPages);
      uiModel.addAttribute("nextPage", TourismUtil.getNextPage(page, maxPages));
      return "touristAgency/advs/tayninhtour";
   }
   
   @RequestMapping(value="/incometour", produces = "text/html", method=RequestMethod.GET)
   public String listInComeTour(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, Model uiModel) {
      long count = scheduleTourService.countToursByTourType(TourType.INCOME);
      page = page == null ? 1 : page; 
      int sizeNo = size == null ? 25 : size.intValue();
      final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
      uiModel.addAttribute("scheduleTours", scheduleTourService.findToursByTourType(TourType.INCOME, firstResult, sizeNo));
      float nrOfPages = (float) count / sizeNo;
      int maxPages = (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages);
      uiModel.addAttribute("maxPages", maxPages);
      uiModel.addAttribute("nextPage", TourismUtil.getNextPage(page, maxPages));
      return "touristAgency/advs/incometour";
   }

   @RequestMapping(value="/outcometour", produces = "text/html", method=RequestMethod.GET)
   public String listOutComeTour(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, Model uiModel) {
      long count = scheduleTourService.countToursByTourType(TourType.OUTCOME);
      page = page == null ? 1 : page; 
      int sizeNo = size == null ? 25 : size.intValue();
      final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
      uiModel.addAttribute("scheduleTours", scheduleTourService.findToursByTourType(TourType.OUTCOME, firstResult, sizeNo));
      float nrOfPages = (float) count / sizeNo;
      int maxPages = (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages);
      uiModel.addAttribute("maxPages", maxPages);
      uiModel.addAttribute("nextPage", TourismUtil.getNextPage(page, maxPages));

      return "touristAgency/advs/outcometour";
   }
   
   String encodeUrlPathSegment(String pathSegment, HttpServletRequest httpServletRequest) {
      String enc = httpServletRequest.getCharacterEncoding();
      if (enc == null) {
          enc = WebUtils.DEFAULT_CHARACTER_ENCODING;
      }
      try {
          pathSegment = UriUtils.encodePathSegment(pathSegment, enc);
      } catch (UnsupportedEncodingException uee) {}
      return pathSegment;
  }
}
